home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1996 April / Macworld (1996-04).dmg / Shareware World / Entertainment / Arcade / CheeseToast / Source / TopScores.c < prev    next >
Text File  |  1994-02-07  |  4KB  |  220 lines

  1. /****************************************************************************
  2.  * TopScores.c
  3.  *
  4.  *        TopScore Stuff
  5.  *
  6.  * JAB 4/28/93 - 5/93
  7.  ****************************************************************************/
  8. #include "CToast.h"
  9.  
  10. ScoreRecord    scoreRecord[MaxScoreRecords];
  11. short        gLastHighScore=-1;
  12.  
  13. void LoadTopScores()
  14. {
  15.     Handle    h;
  16.     Ptr        p;
  17.     short    i;
  18.     h = Get1Resource(TopScoreResType,128);
  19.     if (h != NULL) {
  20.         p = *h;
  21.         for (i = 0; i < MaxScoreRecords; ++i) {
  22.             scoreRecord[i].score = *((long *) p);    p += sizeof(long);
  23.             scoreRecord[i].level = *((short *) p);    p += sizeof(short);
  24.             BlockMove(p,scoreRecord[i].name,p[0]+1);
  25.             p += p[0]+1+((p[0]&1)? 0 : 1);
  26.         }
  27.         ReleaseResource(h);
  28.     }
  29. }
  30.  
  31. void IntegrateScore(long score, short level)
  32. {
  33.     Handle    h,h2;
  34.     Ptr        p;
  35.     short    i,j,id;
  36.     if (score > scoreRecord[MaxScoreRecords-1].score) {
  37.         if (MyRandom(2) == 0)
  38.             PlaySound(S_HighScore1, 5);
  39.         else
  40.             PlaySound(S_HighScore2, 5);
  41.         // Adjust Scores
  42.         for (i = MaxScoreRecords - 1; i >= 0; --i) {
  43.             if (scoreRecord[i].score > score)
  44.                 break;
  45.         }
  46.         for (j = MaxScoreRecords - 1; j > i; --j)
  47.             scoreRecord[j] = scoreRecord[j-1];
  48.         ++i;
  49.         gLastHighScore = i;
  50.         scoreRecord[i].score = score;
  51.         scoreRecord[i].level = level;
  52.         // Get the person's name...
  53.         GetUserName(gPrefs.userName);
  54.         SavePreferences();
  55.         BlockMove(gPrefs.userName, scoreRecord[i].name, gPrefs.userName[0]+1);
  56.  
  57.         // Save Scores
  58.         h = NewHandle(sizeof(ScoreRecord) * MaxScoreRecords);
  59.         if (h) {
  60.             while ((h2 = GetResource(TopScoreResType, 128)) != NULL) {
  61.                 RmveResource(h2);
  62.                 DisposHandle(h2);
  63.             }
  64.  
  65.             p = *h;
  66.             for (i = 0; i < MaxScoreRecords; ++i) {
  67.                 *((long *) p) = scoreRecord[i].score;    p += sizeof(long);
  68.                 *((short *) p) = scoreRecord[i].level;    p += sizeof(short);
  69.                 BlockMove(scoreRecord[i].name,p,scoreRecord[i].name[0]+1);
  70.                 p += p[0]+1+((p[0]&1)? 0 : 1);
  71.  
  72.             }
  73.             SetHandleSize(h,(long)p-(long) *h);
  74.             AddResource(h,TopScoreResType,128,"\pTop Scores");
  75.             WriteResource(h);
  76.             ReleaseResource(h);
  77.         }
  78.     }
  79.     else
  80.         PlaySound(S_Loser, 5);
  81. }
  82.  
  83. void DisplayTopScores()
  84. {
  85.     short        i;
  86.     RGBColor    fc,bc;
  87.     Rect        r;
  88.     short        x,y;
  89.     short        topY,leftX;
  90.     static StringPtr    titleStr = "\pCheeze Whizzes";
  91.  
  92.     TextFont(geneva);
  93.     TextSize(14);
  94.     TextMode(srcOr);
  95.     TextFace(bold);
  96.  
  97.     if (g12InchMode)
  98.         topY = 264;
  99.     else
  100.         topY = 354;
  101.     
  102.     x = 157-StringWidth(titleStr)/2;
  103.     y = topY;
  104.  
  105.     fc.red = 0x0000;
  106.     fc.green = 0x0000; 
  107.     fc.blue = 0x9999;
  108.     RGBForeColor(&fc);
  109.     MoveTo(x+2,y+2);
  110.     DrawString(titleStr);
  111.  
  112.     fc.red = 0x0000;
  113.     fc.green = 0x0000; 
  114.     fc.blue = 0xFFFF;
  115.     RGBForeColor(&fc);
  116.     MoveTo(x+1,y+1);
  117.     DrawString(titleStr);
  118.  
  119.     fc.red = 0xEEEE;
  120.     fc.green = 0xFEEE; 
  121.     fc.blue = 0xFFFF;
  122.     RGBForeColor(&fc);
  123.     MoveTo(x,y);
  124.     DrawString(titleStr);
  125.     
  126.     TextFont(systemFont);
  127.     TextSize(12);
  128.     
  129.     fc.red = 0x0000;
  130.     fc.green = 0xFFFF; 
  131.     fc.blue = 0x0000;
  132.     RGBForeColor(&fc);
  133.     
  134.     SetRect(&r, 50,359, 265,509);
  135. //    FrameRect(&r);
  136.  
  137.     for (i = 0; i < MaxScoreRecords; ++i) {
  138.         if (i == gLastHighScore) {
  139.             fc.red = 0x9999;
  140.             fc.green = 0xBBBB; 
  141.             fc.blue = 0xFFFF;
  142.             RGBForeColor(&fc);
  143.         }
  144.         else {
  145.  
  146. // Black 0x0000     Medium 0x8888     White 0xFFFF   0 1 2 3 4 5 6 7 8 9 A B C D E F
  147.             fc.red = 0x8888;
  148.             fc.green = 0x8888; 
  149.             fc.blue = 0x8888;
  150.             RGBForeColor(&fc);
  151.         }
  152.         PrintfXY(32+24,topY+20+14*i,"%-20.*s",scoreRecord[i].name[0],&scoreRecord[i].name[1]);
  153.         PrintfXY(190,topY+20+14*i,"%d", scoreRecord[i].level);
  154.         PrintfXY(220,topY+20+14*i,"%ld",scoreRecord[i].score);
  155.     }
  156.  
  157.     fc.red = fc.green = fc.blue = 0x0000;
  158.     RGBForeColor(&fc);
  159. }
  160.  
  161. #define NameDLOG    128
  162. #define NameField    2
  163.  
  164. pascal    Boolean    UserNameDialogHook(WindowPtr dp,EventRecord *ep,int *ip)
  165. {
  166.     char    tempChar;
  167.     switch(ep->what) {
  168.         case keyDown:
  169.             tempChar = ep->message & charCodeMask;
  170.             if (tempChar==10 || tempChar==13 || tempChar==3) {
  171.                 *ip = OK;
  172.                 return TRUE;
  173.             }
  174.             return FALSE;
  175.         case updateEvt:
  176.             break;
  177.         default:
  178.             break;
  179.     }
  180.     return FALSE;
  181. }
  182.  
  183. void GetUserName(StringPtr name)
  184. {
  185.     GrafPtr        savePort;
  186.     DialogPtr    dp;
  187.     short        itemHit,t;
  188.     Handle        h;
  189.     Rect        r;
  190.     
  191.     GetPort(&savePort);
  192.     if ((dp = GetNewDialog(NameDLOG,NULL,(WindowPtr) -1)) == NULL)
  193.         return;
  194.  
  195.  
  196.     ShowWindow(dp);
  197.     SetPort(dp);
  198.  
  199.     GetDItem(dp,NameField,&t,&h,&r);
  200.     SetIText(h,name);
  201.     SetDItem(dp,NameField,t,h,&r);
  202.     SelIText(dp,NameField,0,32767);
  203.  
  204.     ShowCursor();
  205.  
  206.     do {
  207.         ModalDialog((ProcPtr) UserNameDialogHook, &itemHit);
  208.     } while (itemHit != OK);
  209.  
  210.     HideCursor();
  211.  
  212.     GetDItem(dp,NameField,&t,&h,&r);
  213.     GetIText(h,name);
  214.  
  215.     DisposDialog(dp);
  216.  
  217.     SetPort(savePort);
  218.  
  219.     MyCopyBits();
  220. }